package com.android.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;

public class FileManager {

	/**
	 * This method copies a file from an InputStream to an OutputStream.
	 * **/	
	public static boolean copyFile(InputStream in, OutputStream out) throws IOException {
	    byte[] buffer = new byte[1024];
	    int read;
	    while((read = in.read(buffer)) != -1){
	      out.write(buffer, 0, read);
	    }
	    return true;
	}

	/**
	 * This method deletes a file from an InputStream to an OutputStream.
	 * **/	
	//Change!
	public static void deleteFile(Context mContext, String filename) throws IOException {
		PackageManager pm = mContext.getPackageManager();
		try {
			String dataDir = pm.getApplicationInfo(mContext.getPackageName(), 0).dataDir + "/";
			
			//Changing file privileges
			Runtime.getRuntime().exec("rm " + dataDir + filename);

		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

	}

    /**
     * This method checks if a file exists in the given directory
     * **/
    @SuppressWarnings("unused")
	public boolean fileExists(Context context, String filename, String dirName) {
    	boolean exists = false;
    	
    	//Gets the directory, creating it if needed
		File dir = context.getDir(dirName, Context.MODE_PRIVATE);

		//Gets all files inside this directory
		File[] filesList = dir.listFiles();
		
		if (filesList.length == 0)
			exists = false;
		
		for (int i = 0; i < filesList.length; i++) {
			
			if (filesList[i].isFile() && filesList[i].getName().equals(dir))	
				exists = true;
				break;
		}
		
		return exists;
    }
    
    
    public static void copyMediaFile(Context mContext, String AssetPath) throws IOException {
    	AssetManager assetManager = mContext.getAssets();
    	
    	String[] files = null;
 	   	try {
 	   		files = assetManager.list(AssetPath);
 	   		
	    } catch (IOException e) {
 	    	android.util.Log.e("IOException", e.getMessage());
 	    }
 	    
 	   	for (int i = 0; i < files.length; i++) {
 	        InputStream in = null;
 	        OutputStream out = null;
 	
 	        try {
				PackageManager pm = mContext.getPackageManager();
				String dataDir = 	pm.getApplicationInfo(mContext.getPackageName(), 0).dataDir + "/";

				in = assetManager.open(AssetPath + "/" + files[i]);
				out = new FileOutputStream(dataDir + files[i]);
				
				//Changing file privileges
				Runtime.getRuntime().exec("chmod 777 " + dataDir + files[i]);
				
				//Copying file
				copyFile(in, out);
 	          
				in.close();
				out.flush();
				out.close();
 	         
				in = null;
				out = null;
 	          
 	        } catch(Exception e) {
 	        	android.util.Log.e("error", e.getMessage());
				e.printStackTrace();
 	        }
 	    }
    }

}
